import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/components/login'



Vue.use(VueRouter)

const router = new VueRouter({
  mode: "history",
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login',component: login },
    { path: '/home',component: () => import("@/components/home"),redirect:"/welcome",
    children:[
      {path:"/welcome",component: () => import("@/components/welcome")},
      {path:"/users",component: () => import("@/components/user")},
      {path:"/rights",component: () => import("@/components/power/rights")},
      {path:"/roles",component: () => import("@/components/power/roles")},
      {path:"/categories",component: () => import("@/components/goods/categories")},
      {path:"/params",component: () => import("@/components/goods/params")},
      {path:"/goods",component: () => import("@/components/goods/list")},
      {path:"/goods/Add",component: () => import("@/components/goods/Add")},
      {path:"/orders",component: () => import("@/components/order/Order")},
      {path:"/reports",component: () => import("@/components/report/report")},
    ]
  },
  ]
})
// 路由守卫
router.beforeEach((to,from,next)=>{
  // to表示要去哪里
  // from：从哪里来
  // next()放行
  //next('/login')没有登录则强制跳转到登录页面
  if(to.path==="/login"){
    next()
  }
  var userInfo = window.sessionStorage.getItem("token")
  if(userInfo){
    next()
  }else{
    next('/login')
  }
})

export default router
